
<template>
  <div class="homepage-hero-module">
    <div class="video-container">
      <div :style="fixStyle"
           class="filter">
        <div id="particles-js">
          <div class="login">
            <div class="right_text">
              <span class="login_text">社区管理系统</span>
            </div>
            <el-form ref="loginFormRef"
                     :model="userInfo"
                     :rules="loginFormRules"
                     class="login_form">
              <!-- 账号 -->
              <el-form-item prop="userName">
                <el-input v-model="userInfo.userName"
                          placeholder="用户名"
                          clearable
                          prefix-icon="iconfont icon-user">
                </el-input>
              </el-form-item>
              <!-- 密码 -->
              <el-form-item prop="pwd">
                <el-input type="pwd"
                          v-model="userInfo.pwd"
                          placeholder="密码"
                          show-password
                          prefix-icon="iconfont icon-3702mima"
                          clearable></el-input>
              </el-form-item>
              <!-- 验证码 -->
              <el-form-item prop="identifyCode1">
                <el-row :gutter="15">
                  <el-col :span="14">
                    <el-input v-model="userInfo.identifyCode1"
                              placeholder="请输入验证码"
                              prefix-icon="iconfont icon-danju"
                              clearable></el-input>
                  </el-col>
                  <el-col :span="10">
                    <div @click="refreshCode">
                      <Identify :identifyCode="identifyCode"
                                :contentWidth="120"
                                :contentHeight="40"></Identify>
                    </div>
                  </el-col>
                </el-row>
              </el-form-item>
              <!-- 按钮 -->
              <el-form-item class="btns">
                <el-button type="primary"
                           @click="login">登录</el-button>
                <el-button type="info"
                           @click="resetLoginForm">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
      <video :style="fixStyle"
             autoplay
             loop
             muted
             class="fillWidth"
             v-on:canplay="canplay">
        <source src="../assets/img/Surfers.mp4"
                type="video/mp4" />
      </video>
    </div>
  </div>
</template>
<script>
import Identify from '@/components/Identify.vue'
import { mapState } from "vuex";
export default {
  name: 'Video',
  components: {
    Identify,
  },
  data () {
    //自定义验证验证码规则
    let checkCode = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入验证码"));
      } else if (value !== this.identifyCode) {
        callback(new Error("验证码输入错误"));
      } else {
        callback();
      }
    };
    return {
      vedioCanPlay: false,
      fixStyle: '',
      userInfo: {
        userName: 'admin',
        pwd: '123456',
        // 填入的验证码
        identifyCode1: ''
      },
      identifyCodes: '1234567890', //验证码类型
      // 自动生成的图片验证码
      identifyCode: '',
      // 表单验证规则
      loginFormRules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 11, message: "长度在 3 到 10 个字符", trigger: "blur" },
        ],
        pwd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 16,
            message: "长度在 3 到 16 个字符",
            trigger: "blur",
          },
        ],
        identifyCode1: [{ validator: checkCode, trigger: "blur" }],
      },

    }
  },
  methods: {
    canplay () {
      this.vedioCanPlay = true
    },
    // 生成随机数
    randomNum (min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },
    // 切换验证码
    refreshCode () {
      this.userInfo.identifyCode1 = "";
      this.makeCode(this.identifyCodes, 4);
    },
    // 生成四位随机验证码
    makeCode (o, l) {
      this.identifyCode = ''
      for (let i = 0; i < l; i++) {
        this.identifyCode +=
          this.identifyCodes[this.randomNum(0, this.identifyCodes.length)];
      }
      this.userInfo.identifyCode1 = this.identifyCode
    },
    // 点击登录事件
    // login() {
    //   // 验证表单
    //   this.$refs.loginFormRef.validate(async valid => {
    //     if (!valid) return
    //     if(this.userInfo.username==='admin'&&this.userInfo.password==='123456'){
    //       this.$message.success('登录成功')
    //       this.$router.push('/center/welcome')
    //     }
    //   })
    // },
    // 点击登录事件
    login () {
      // 验证表单
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post("UserLoginAction/WebLogin", this.userInfo)
        console.log(res)
        if (!res.success) return this.$message.success('登录失败')
        this.$message.success('登录成功')
        let result = res.rows[0]
        window.sessionStorage.setItem('token', result.token)
        window.sessionStorage.setItem('userInfo', JSON.stringify(result.user))
        window.sessionStorage.setItem('userId', result.user.id)
        this.$store.dispatch("set_token", result.token);
        this.$store.dispatch("set_userInfo", result.user);
        this.$router.push('/center/welcome')
        // if (res.success && this.userInfo.userName !== 'admin')
        //   this.$router.push('/login/select')
      })
    },
    // 重置按钮
    resetLoginForm () {
      // 先获取到表单的实例对象
      this.$refs.loginFormRef.resetFields()
    }

  },
  mounted: function () {   //屏幕自适应
    window.onresize = () => {
      const windowWidth = document.body.clientWidth
      const windowHeight = document.body.clientHeight
      const windowAspectRatio = windowHeight / windowWidth
      let videoWidth
      let videoHeight
      if (windowAspectRatio < 0.5625) {
        videoWidth = windowWidth
        videoHeight = videoWidth * 0.5625
        this.fixStyle = {
          height: windowWidth * 0.5625 + 'px',
          width: windowWidth + 'px',
          'margin-bottom': (windowHeight - videoHeight) / 2 + 'px',
          'margin-left': 'initial'
        }
      } else {
        videoHeight = windowHeight
        videoWidth = videoHeight / 0.5625
        this.fixStyle = {
          height: windowHeight + 'px',
          width: windowHeight / 0.5625 + 'px',
          'margin-left': (windowWidth - videoWidth) / 2 + 'px',
          'margin-bottom': 'initial'
        }
      }
    }
    window.onresize()
    this.identifyCode = "";
    this.makeCode(this.identifyCodes, 4);
  }
}
</script>

<style scoped>
.login {
  background: rgba(255, 255, 255, 0.2);
  z-index: 2;
  position: absolute;
  width: 350px;
  border-radius: 5px;
  height: 500px;
  box-shadow: 0px 0px 5px #333333;
  top: 50%;
  left: 50%;
  margin-top: -280px;
  margin-left: -175px;
  transition: all 1s;
  -moz-transition: all 1s; /* Firefox 4 */
  -webkit-transition: all 1s; /* Safari 和 Chrome */
  -o-transition: all 1s; /* Opera */
}

.homepage-hero-module,
.video-container {
  position: relative;
  height: 100vh;
  overflow: hidden;
}

.video-container .filter {
  z-index: 1;
  position: absolute;
  background: rgba(0, 0, 0, 0.4);
  width: 100%;
}
.right_text {
  box-sizing: border-box;
  width: 100%;
  height: 60px;
  padding: 10px;
  margin-top: 50px;
  text-align: center;
}
.login_text {
  font-size: 20px;
  color: #fff;
}
.fillWidth {
  width: 100%;
}
.login_form {
  position: absolute;
  width: 100%;
  padding: 0 20px;
  margin-top: 60px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>